/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.kth.lwr.jeat.core;

import java.util.HashMap;
import java.util.Map;
import javax.measure.quantity.Dimensionless;
import javax.measure.quantity.Quantity;
import javax.measure.unit.BaseUnit;
import javax.measure.unit.Unit;
import org.jscience.physics.amount.Amount;
import se.kth.lwr.jeat.samples.onsitetreatment.Units;

/**A standard {@link ImpactModel} implementation for Eutrofication, based on 
 * the phosphate equivalents concept (Guinee 2002)
 *
 * @author davidev
 */
public class EutroficationModel extends ImpactModel {
    
    public static final Unit<Dimensionless> PO4equivalent=new BaseUnit<>("PO4eq");

    //field for storage of Eutrophication factors
    private Map<EmissionCategory, Amount<Dimensionless>> factors;
    
    
    /**
     * creates a new EutrophicationModel
     */
    public EutroficationModel() {
        super("Eutropgication potential", "Eutrophication potential model (Guinee 2002)");


        factors = new HashMap<>();
        factors.put(EmissionCategory.P, Amount.valueOf(3.06, Units.PO4Eqvivalent));
        factors.put(EmissionCategory.N, Amount.valueOf(0.42, Units.PO4Eqvivalent));
    }
    
   /**
    * Returns the impact caused by the passed {@link Inventoriable}
    * @param inventoriable
    * @return value of the impact stated in PO4-equivalents
    */

    @Override
    public Amount<? extends Quantity> getImpact(Inventoriable inventoriable) {
        Inventory inv = inventoriable.getInventory();
        Amount<? extends Quantity> result = null;
        for (EmissionCategory cat : factors.keySet()) {
            //Avoid null values
            Amount<? extends Quantity> value = inv.getEmissionToWater(cat);
            if (value != null) {
                //let first match determine the unit ot the result
                if (result == null) {
                    result = value.times(factors.get(cat));
                }
                //then continue addition
                result = result.plus(value.times(factors.get(cat)));
            }
           
        }
        

        return result;


    }
    
    /**
     * 
     * @return the unit specifying the value of the impact
     */

    @Override
    public Unit getUnit() {
        return PO4equivalent;
    }


}
